package com.internetCafes.spms.web.sys.controller;

import com.internetCafes.spms.common.CommonConstants;
import com.internetCafes.spms.common.utils.DateUtils;
import com.internetCafes.spms.common.utils.ShiroUtils;
import com.internetCafes.spms.core.model.pageModel.DataGrid;
import com.internetCafes.spms.core.utils.Query;
import com.internetCafes.spms.core.utils.R;
import com.internetCafes.spms.web.sys.model.PolicyInfo;
import com.internetCafes.spms.web.sys.service.PolicyInfoService;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author LinYe
 * @date 2020/10/29
 */
@RestController
@RequestMapping("/sys/policy")
public class PolicyInfoController {

    @Autowired
    PolicyInfoService policyInfoService;

    @GetMapping("/list")
    /*@RequiresPermissions("sys:policy:list")*/
    public R list(@RequestParam Map<String, Object> params, HttpServletRequest request){
        Query query = new Query(params);
        params = new HashMap<>();

        String title = request.getParameter("title");
        String createTime = request.getParameter("createTime");
        String url = request.getParameter("url");
        String createBy = request.getParameter("createBy");
        String author = request.getParameter("author");
        String source = request.getParameter("source");
        String sort = request.getParameter("sort");
        String visible = request.getParameter("visible");

        if (StringUtils.isNotBlank(title)) {
            params.put("title",title);
        }

        if (StringUtils.isNotBlank(createTime)) {
            params.put("createTime", DateUtils.strToDate(createTime));
        }

        if (StringUtils.isNotBlank(url)) {
            params.put("url", url);
        }

        if (StringUtils.isNotBlank(createBy)) {
            params.put("createBy", Long.parseLong(createBy));
        }

        if (StringUtils.isNotBlank(author)) {
            params.put("author", author);
        }

        if (StringUtils.isNotBlank(source)) {
            params.put("source", source);
        }

        if (StringUtils.isNotBlank(sort)) {
            params.put("sort", Long.parseLong(sort));
        }

        if (StringUtils.isNotBlank(visible)) {
            params.put("visible", Integer.parseInt(visible));
        }

        DataGrid dataGrid = policyInfoService.dataGrid(params, query.getPage(), query.getLimit());

        return R.ok().put("page", dataGrid);
    }

    @PostMapping("/save")
    @RequiresPermissions("sys:policy:save")
    public R save(PolicyInfo policyInfo){
        policyInfo.setCreateBy(ShiroUtils.getUserId());
        policyInfo.setCreateTime(new Date());
        policyInfoService.savePolicy(policyInfo);
        return R.ok();
    }

    @GetMapping("/delete/{id}")
    @RequiresPermissions("sys:policy:delete")
    public R delete(@PathVariable("id")Long id){
        policyInfoService.deletePolicy(id);
        return R.ok();
    }

    @PostMapping("/update")
    @RequiresPermissions("sys:policy:update")
    public R update(PolicyInfo policyInfo){
        policyInfoService.updatePolicy(policyInfo);
        return R.ok();
    }

    @GetMapping("/getDetail/{id}")
    @RequiresPermissions("sys:policy:info")
    public R getDetail(@PathVariable("id")Long id){
        PolicyInfo policyDetail = policyInfoService.getPolicyDetail(id);
        R ok = R.ok();
        ok.put("data",policyDetail);
        return ok;
    }
}
